﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace GridViewDragDrop
{
    public partial class Form1 : Form
    {
        private BindingSource bs;

        private List<MyItem> items = new List<MyItem> {
            new MyItem {Id = 0, Name = "Hello"},
            new MyItem {Id = 1, Name = "World"},
            new MyItem {Id = 2, Name = "Foo"},
            new MyItem {Id = 3, Name = "Bar"},
            new MyItem {Id = 4, Name = "Scott"},
            new MyItem {Id = 5, Name = "Tiger"},
        };

        public Form1()
        {
            InitializeComponent();

            bs = new BindingSource(items, string.Empty);
            gridView1.DataSource = bs;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int position = bs.Position;
            if (position == 0) {
                MessageBox.Show("Already Top Item!");
                return;
            }

            bs.RaiseListChangedEvents = false;

            MyItem current = (MyItem)bs.Current;
            bs.Remove(current);

            position--;

            bs.Insert(position, current);
            bs.Position = position;

            bs.RaiseListChangedEvents = true;
            bs.ResetBindings(false);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int position = bs.Position;
            if (position == bs.Count - 1)
            {
                MessageBox.Show("Already Bottom Item!");
                return; 
            }

            bs.RaiseListChangedEvents = false;

            MyItem current = (MyItem)bs.Current;
            bs.Remove(current);

            position++;

            bs.Insert(position, current);
            bs.Position = position;

            bs.RaiseListChangedEvents = true;
            bs.ResetBindings(false);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv";

            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                string fileName = dialog.FileName;
                UploadExcel(fileName);
            }
        }

        #region Method
        private void UploadExcel(string fileName)
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            //xlRange.Cells[i, j].Value2

            if (xlApp != null)
            {
                try
                {
                    xlWorkbook.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                    xlWorksheet = null;
                }
                catch (Exception ex)
                {
                    xlApp = null;
                    MessageBox.Show("Unable to release the Object " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

    public class MyItem
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }
}
